package ntut.andengine.game;

import ntut.andengine.physics.Bar;
import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel14 extends GameLevel {

GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(240, 740));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel14.this, GameLevel17.class);
				}			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {}
		
		});
        
        final Bar bar1 = PhysicalFactory.CreateBar(this, new Vector2(80, 140+160), ComponentColor.Blue, new Vector2(0, -20), false);
        final Bar bar2 = PhysicalFactory.CreateBar(this, new Vector2(240, 140), ComponentColor.Red, new Vector2(0, -20), false);
        final Bar bar3 = PhysicalFactory.CreateBar(this, new Vector2(400, 140+160), ComponentColor.Blue, new Vector2(0, -20), false);
        final Bar bar4 = PhysicalFactory.CreateBar(this, new Vector2(20+160, 240), ComponentColor.Red, new Vector2(20, 0), true);
        final Bar bar5 = PhysicalFactory.CreateBar(this, new Vector2(20, 400), ComponentColor.Blue, new Vector2(20, 0), true);
        final Bar bar6 = PhysicalFactory.CreateBar(this, new Vector2(20+160, 560), ComponentColor.Red, new Vector2(20, 0), true);
        final Bar bar7 = PhysicalFactory.CreateBar(this, new Vector2(460-160, 240), ComponentColor.Red, new Vector2(-20, 0), true);
        final Bar bar8 = PhysicalFactory.CreateBar(this, new Vector2(460, 400), ComponentColor.Blue, new Vector2(-20, 0), true);
        final Bar bar9 = PhysicalFactory.CreateBar(this, new Vector2(460-160, 560), ComponentColor.Red, new Vector2(-20, 0), true);
        final Bar bar10 = PhysicalFactory.CreateBar(this, new Vector2(80, 660-160), ComponentColor.Blue, new Vector2(0, 20), false);
        final Bar bar11 = PhysicalFactory.CreateBar(this, new Vector2(240, 660), ComponentColor.Red, new Vector2(0, 20), false);
        final Bar bar12 = PhysicalFactory.CreateBar(this, new Vector2(400, 660-160), ComponentColor.Blue, new Vector2(0, 20), false);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(240, 60), ComponentColor.Red);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	
		mScene.registerUpdateHandler(new IUpdateHandler(){
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				
				if (bar1.getPosition().y > 660 || bar1.getPosition().y < 140){
					if(bar1.getPosition().y > 660 ){
		            	bar1.setPosition(new Vector2(bar1.getPosition().x, 660));
		            }
		            if(bar1.getPosition().y < 140 ){
		            	bar1.setPosition(new Vector2(bar1.getPosition().x, 140));
		            }
		            bar1.setVelocity(bar1.getVelocity().mul(-1));
				}
				if (bar2.getPosition().y > 660 || bar2.getPosition().y < 140){
					if(bar2.getPosition().y > 660 ){
		            	bar2.setPosition(new Vector2(bar2.getPosition().x, 660));
		            }
		            if(bar2.getPosition().y < 140 ){
		            	bar2.setPosition(new Vector2(bar2.getPosition().x, 140));
		            }
		            bar2.setVelocity(bar2.getVelocity().mul(-1));
				}
				if (bar3.getPosition().y > 660 || bar3.getPosition().y < 140){
					if(bar3.getPosition().y > 660 ){
		            	bar3.setPosition(new Vector2(bar3.getPosition().x, 660));
		            }
		            if(bar3.getPosition().y < 140 ){
		            	bar3.setPosition(new Vector2(bar3.getPosition().x, 140));
		            }
		            bar3.setVelocity(bar3.getVelocity().mul(-1));
				}
				if (bar4.getPosition().x > 460 || bar4.getPosition().x < 20){
					if(bar4.getPosition().x > 460 ){
		            	bar4.setPosition(new Vector2(460, bar4.getPosition().y));
		            }
		            if(bar4.getPosition().x < 20 ){
		            	bar4.setPosition(new Vector2(20, bar4.getPosition().y));
		            }
		            bar4.setVelocity(bar4.getVelocity().mul(-1));
				}
				if (bar5.getPosition().x > 460 || bar5.getPosition().x < 20){
					if(bar5.getPosition().x > 460 ){
		            	bar5.setPosition(new Vector2(460, bar5.getPosition().y));
		            }
		            if(bar5.getPosition().x < 20 ){
		            	bar5.setPosition(new Vector2(20, bar5.getPosition().y));
		            }
		            bar5.setVelocity(bar5.getVelocity().mul(-1));
				}
				if (bar6.getPosition().x > 460 || bar6.getPosition().x < 20){
					if(bar6.getPosition().x > 460 ){
		            	bar6.setPosition(new Vector2(460, bar6.getPosition().y));
		            }
		            if(bar6.getPosition().x < 20 ){
		            	bar6.setPosition(new Vector2(20, bar6.getPosition().y));
		            }
		            bar6.setVelocity(bar6.getVelocity().mul(-1));
				}
				if (bar7.getPosition().x > 460 || bar7.getPosition().x < 20){
					if(bar7.getPosition().x > 460 ){
		            	bar7.setPosition(new Vector2(460, bar7.getPosition().y));
		            }
		            if(bar7.getPosition().x < 20 ){
		            	bar7.setPosition(new Vector2(20, bar7.getPosition().y));
		            }
		            bar7.setVelocity(bar7.getVelocity().mul(-1));
				}
				if (bar8.getPosition().x > 460 || bar8.getPosition().x < 20){
					if(bar8.getPosition().x > 460 ){
		            	bar8.setPosition(new Vector2(460, bar8.getPosition().y));
		            }
		            if(bar8.getPosition().x < 20 ){
		            	bar8.setPosition(new Vector2(20, bar8.getPosition().y));
		            }
		            bar8.setVelocity(bar8.getVelocity().mul(-1));
				}
				if (bar9.getPosition().x > 460 || bar9.getPosition().x < 20){
					if(bar9.getPosition().x > 460 ){
		            	bar9.setPosition(new Vector2(460, bar9.getPosition().y));
		            }
		            if(bar9.getPosition().x < 20 ){
		            	bar9.setPosition(new Vector2(20, bar9.getPosition().y));
		            }
		            bar9.setVelocity(bar9.getVelocity().mul(-1));
				}
				if (bar10.getPosition().y > 660 || bar10.getPosition().y < 140){
					if(bar10.getPosition().y > 660 ){
		            	bar10.setPosition(new Vector2(bar10.getPosition().x, 660));
		            }
		            if(bar10.getPosition().y < 140 ){
		            	bar10.setPosition(new Vector2(bar10.getPosition().x, 140));
		            }
		            bar10.setVelocity(bar10.getVelocity().mul(-1));
				}
				if (bar11.getPosition().y > 660 || bar11.getPosition().y < 140){
					if(bar11.getPosition().y > 660 ){
		            	bar11.setPosition(new Vector2(bar11.getPosition().x, 660));
		            }
		            if(bar11.getPosition().y < 140 ){
		            	bar11.setPosition(new Vector2(bar11.getPosition().x, 140));
		            }
		            bar11.setVelocity(bar11.getVelocity().mul(-1));
				}
				if (bar12.getPosition().y > 660 || bar12.getPosition().y < 140){
					if(bar12.getPosition().y > 660 ){
		            	bar12.setPosition(new Vector2(bar12.getPosition().x, 660));
		            }
		            if(bar12.getPosition().y < 140 ){
		            	bar12.setPosition(new Vector2(bar12.getPosition().x, 140));
		            }
		            bar12.setVelocity(bar12.getVelocity().mul(-1));
				}
			}

			@Override
			public void reset() {}
			
		});

		return mScene;
	}
}
